package me.zhengjie.pm.taskprogress.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.pm.taskprogress.domain.TaskProgress;
import me.zhengjie.pm.taskprogress.service.TaskProgressService;
import me.zhengjie.pm.taskprogress.service.dto.TaskProgressQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author yuxj
* @date 2020-04-14
*/
@Api(tags = "研发任务进度管理")
@RestController
@RequestMapping("/api/taskProgress")
public class TaskProgressController {

    private final TaskProgressService taskProgressService;

    public TaskProgressController(TaskProgressService taskProgressService) {
        this.taskProgressService = taskProgressService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('taskProgress:list')")
    public void download(HttpServletResponse response, TaskProgressQueryCriteria criteria) throws IOException {
        taskProgressService.download(taskProgressService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询研发任务进度")
    @ApiOperation("查询研发任务进度")
    @PreAuthorize("@el.check('taskProgress:list')")
    public ResponseEntity<Object> getTaskProgresss(TaskProgressQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(taskProgressService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增研发任务进度")
    @ApiOperation("新增研发任务进度")
    @PreAuthorize("@el.check('taskProgress:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody TaskProgress resources){
        return new ResponseEntity<>(taskProgressService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改研发任务进度")
    @ApiOperation("修改研发任务进度")
    @PreAuthorize("@el.check('taskProgress:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody TaskProgress resources){
        taskProgressService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除研发任务进度")
    @ApiOperation("删除研发任务进度")
    @PreAuthorize("@el.check('taskProgress:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        taskProgressService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}